<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="会员名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入会员名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="精确用户名称" prop="exact_name">
        <el-input
          v-model="queryParams.exact_name"
          placeholder="请输入精确用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册时间">
        <el-date-picker
          v-model="registerTimeRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openSelectUser"
          v-hasPermi="['customer:announcement:config']"
        >发送会员私信
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="cancelAuthUserAll"
          v-hasPermi="['customer:announcement:config']"
        >批量取消发送
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="会员id" align="center" key="id" prop="id"
                       :show-overflow-tooltip="true" width="80"
      />
      <el-table-column label="会员名称" align="center" key="name" prop="name"
                       :show-overflow-tooltip="true" width="160"
      />
      <el-table-column label="会员手机号" align="center" key="phone" prop="phone"
                       :show-overflow-tooltip="true" width="160"
      />
      <el-table-column label="发送时间" align="center" key="send_time" prop="send_time" width="160"/>
      <el-table-column label="注册时间" align="center" key="create_time" prop="create_time" width="160"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column prop="quantification_status" label="量化状态">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.quantification_status"/>
        </template>
      </el-table-column>
      <el-table-column prop="withdraw_status" label="提现状态">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.withdraw_status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="cancelAuthUser(scope.row)"
            v-hasPermi="['customer:announcement:config']"
          >取消授权
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />
    <select-customer ref="select" :app_id="queryParams.app_id"
                     :no_app_announcement_id="queryParams.app_announcement_id" @ok="handleQuery"/>
  </div>
</template>

<script>
import {listUser, sendCancel} from "@/api/customer/customerAnnouncement";
import selectCustomer from "./selectCustomer";

export default {
  name: "SendCustomer",
  dicts: ['sys_normal_disable'],
  components: {selectCustomer},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中用户组
      user_ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 日期范围
      registerTimeRange: [],
      // 查询参数
      queryParams: {
        page: 1,
        page_size: 10,
        app_id: undefined,
        app_announcement_id: undefined,
        name: undefined,
        exact_name: undefined,
        phone: undefined,
      },
    };
  },
  created() {
    const app_id = this.$route.params.appId;
    this.queryParams.app_id = app_id;
    const app_announcement_id = this.$route.params.appAnnouncementId;
    this.queryParams.app_announcement_id = app_announcement_id;
    this.getList();
  },
  methods: {
    /** 查询授权用户列表 */
    getList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.registerTimeRange, '_register_time')).then(response => {
          this.userList = response.data;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 返回按钮
    handleClose() {
      const obj = {path: "/customer/customerAnnouncement"};
      this.$tab.closeOpenPage(obj);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      let appId = this.queryParams.app_id
      this.resetForm('queryForm')
      this.queryParams.app_id = appId
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.user_ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 打开授权用户表弹窗 */
    openSelectUser() {
      this.$refs.select.show();
    },
    /** 取消发送按钮操作 */
    cancelAuthUser(row) {
      const app_announcement_id = this.queryParams.app_announcement_id;
      this.$modal.confirm('确认要取消该用户"' + row.name + '"发送私信吗？').then(function () {
        return sendCancel({app_announcement_id: app_announcement_id, customer_ids: row.id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消发送成功");
      }).catch(() => {
      });
    },
    /** 批量取消授权按钮操作 */
    cancelAuthUserAll(row) {
      const app_announcement_id = this.queryParams.app_announcement_id;
      const user_ids = this.user_ids.join(",");
      this.$modal.confirm('是否取消选中用户发送私信？').then(function () {
        return sendCancel({app_announcement_id: app_announcement_id, customer_ids: user_ids});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消发送功");
      }).catch(() => {
      });
    }
  }
};
</script>
